package com.wyzz.hr.util;

import java.io.IOException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class TextTag extends BodyTagSupport {

	public TextTag() {
	}

	public void setCounts(int c) {
		counts = c;
	}

	public int doStartTag() throws JspTagException {
		return counts <= 0 ? 1 : 2;
	}

	public int doEndTag() throws JspTagException {
		try {
			if (bodyContent != null) {
				String body = bodyContent.getString().trim();
				byte bodyBytes[] = body.getBytes();
				if (counts > bodyBytes.length) {
					bodyContent.getEnclosingWriter().write(body);
				} else {
					byte subBytes[] = new byte[counts];
					for (int i = 0; i < counts; i++)
						subBytes[i] = bodyBytes[i];

					int subIndex = (new String(subBytes)).length();
					bodyContent.getEnclosingWriter().write(
							(new StringBuilder()).append("<span title='")
									.append(body).append("'>").append(
											body.substring(0, subIndex))
									.append("...</span>").toString());
				}
			}
		} catch (IOException e) {
			throw new JspTagException(e.getMessage());
		}
		return 6;
	}

	public void doInitBody() throws JspTagException {
	}

	public void setBodyContent(BodyContent bodyContent) {
		this.bodyContent = bodyContent;
	}

	private static final long serialVersionUID = 1L;
	int counts;
}
